package com.von.nhcf.controller;

import com.von.nhcf.constant.NhcfConstant;
import com.von.nhcf.entity.po.MemberPO;
import com.von.nhcf.service.MysqlMemberService;
import com.von.nhcf.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

@RestController
public class MysqlMemberController {
    @Autowired
    MysqlMemberService mysqlMemberService;

    @RequestMapping("/get/member/by/username")
    public ResultEntity<MemberPO> getMemberByUsername(@RequestParam("loginacct") String username) {
        try {
            MemberPO memberPO = mysqlMemberService.getMemberPOByUsername(username);
            return ResultEntity.successWithData(memberPO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failureWithMsg("查询失败");
        }
    }

    @RequestMapping("/save/member/remote")
    public ResultEntity saveMemberRemote(@RequestBody MemberPO memberPO) {
        try {
            mysqlMemberService.saveMemberPO(memberPO);
            return ResultEntity.successWithoutData();
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return ResultEntity.failureWithMsg(NhcfConstant.MESSAGE_LOGIN_ACCOUT_ALREADY_EXISTED);
            }
            return ResultEntity.failureWithMsg(e.getMessage());
        }
    }
}
